﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EximDemo.Models;
using System.IO;

namespace EximDemo.Controllers
{
    public class EximController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public FileContentResult RunExport()
        {
            BizExport export = new BizExport();
            return File(new System.Text.UTF8Encoding().GetBytes(export.RenderExportData()), "text/csv", export.FileName);
        }

        public ActionResult RunImport(HttpPostedFileBase importFile)
        {
            if (importFile != null)
            {
                string filename = System.IO.Path.GetFileName(importFile.FileName);

                string path = Server.MapPath("/Upload/") + GenerateUploadFileName(filename);
                importFile.SaveAs(path);
                importFile.InputStream.Close();

                BizImport import = new BizImport(path);
                import.RunImport();
            }
            return View("Index");
        }

        private static string GenerateUploadFileName(string filename)
        {
            return Path.GetFileNameWithoutExtension(filename)
                + DateTime.Now.ToString("_yyyyMMddhhmmssfff")
                + Path.GetExtension(filename);
        }
    }
}
